package com.lm.survey.logic;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import com.lm.survey.model.ChoiceTemplate;
import com.lm.survey.model.GeneralOptionValue;
import com.lm.survey.model.SingleChoiceTemplate;
import com.lm.survey.util.DBPool;
import com.lm.survey.util.SurveyException;

public class ChoiceLogic {
	static Logger logger = Logger.getLogger(ChoiceLogic.class);

	public static void saveChoice(int itemId, ChoiceTemplate template,
			Connection con) {
	    if(template==null) return ;
		switch (template.getType()) {
		case 1: {
			

		}case 3:{
		    
		}
		case 11:{
		    
		}
		case 6:{
		    new SingleChoiceLogic().saveSingleChoice(itemId,
                    (SingleChoiceTemplate) template, con);
            break;
		}
		case 2: {
		    new SingleChoiceLogic().saveSingleChoice(itemId,
                    (SingleChoiceTemplate) template, con);
		    break;
		}
		}

	}
	
	public static Object   saveOptionalValue(int itemId,int type,HttpServletRequest request){
		switch (type) {
		case 1: {
		   
		}
		case 3:{
		    
		}
		case 11:{
		    
		}
		case 6:{
		    String value = request.getParameter("value");
		    String strWeight = request.getParameter("weight");
		    int weight = 0;
		    if(strWeight==null||strWeight.length()==0){
		        weight=1;
		    }else{
		        weight = Integer.parseInt(request.getParameter("weight"));
		    }
            
            GeneralOptionValue op = new GeneralOptionValue();
            op.setValue(value);
            op.setWeight(weight);
            new SingleChoiceLogic().addOptionalValue(itemId, op);
            return new Integer(op.getId()); 
		}
		case 2: {
		    String value = request.getParameter("value");
		    String strWeight = request.getParameter("weight");
		    int weight = 0;
            if(strWeight==null||strWeight.length()==0){
                weight=1;
            }else{
                weight = Integer.parseInt(request.getParameter("weight"));
            }
            GeneralOptionValue op = new GeneralOptionValue();
            op.setValue(value);
            op.setWeight(weight);
            new SingleChoiceLogic().addOptionalValue(itemId, op);
            return new Integer(op.getId()); 
        }
		}
		
		return null;
	}
	
	
	public static void   updateOptionalValue(int itemId,int type,HttpServletRequest request){
		switch (type) {
		case 1: {
			
			
		}case 3:{
		    
		}
		case 11:{
		    
		}
		case 6:{
		    String value = request.getParameter("value");
            int weight = Integer.parseInt(request.getParameter("weight"));
            GeneralOptionValue op = new GeneralOptionValue();
            op.setValue(value);
            op.setWeight(weight);
            op.setId(Integer.parseInt(request.getParameter("id")));
            new SingleChoiceLogic().updateOptionValue(op);
            break;
		}
		case 2: {
		    String value = request.getParameter("value");
            int weight = Integer.parseInt(request.getParameter("weight"));
            GeneralOptionValue op = new GeneralOptionValue();
            op.setValue(value);
            op.setWeight(weight);
            op.setId(Integer.parseInt(request.getParameter("id")));
            new SingleChoiceLogic().updateOptionValue(op);
            break;
		}
		}
		
	}
	
	public static void   removeOptionalValue(int type,HttpServletRequest request){
		switch (type) {
		case 1: {
			
			
			
		}case 3:{
		    
		}
		case 6:{
		    int id = Integer.parseInt(request.getParameter("id"));
            new SingleChoiceLogic().removeOptionValue(id);
            break;
		}
		case 2: {
		    int id = Integer.parseInt(request.getParameter("id"));
            new SingleChoiceLogic().removeOptionValue(id);
            break;
		}
		case 11: {
            int id = Integer.parseInt(request.getParameter("id"));
            new SingleChoiceLogic().removeOptionValue(id);
            break;
        }
		}
		
	}
	

	public static ChoiceTemplate getTemplate(int itemId, int type,
			Connection con) {
		switch (type) {
		case 1: {
			
			
		}
		case 3:{
		    
		}
		case 6:{
		    return new SingleChoiceLogic().getTemplate(itemId, con);
		}
		
		case 2: {
		    return new SingleChoiceLogic().getTemplate(itemId, con);
		}
		case 11:{
		    return new SingleChoiceLogic().getTemplate(itemId, con);
		}
		}
		return null;
	}

	public static void removeAllChoice(int itemId, int type, Connection con) {

		switch (type) {
		case 1: {
			new SingleChoiceLogic().removeAllOptionValue(itemId, con);

		}
		case 2: {
		    new SingleChoiceLogic().removeAllOptionValue(itemId, con);
		}
		case 11: {
            new SingleChoiceLogic().removeAllOptionValue(itemId, con);
        }
		}

	}

}
